<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar style="background-color: #49aefa">
      <template #title>
        <van-button
          type="info"
          icon="search"
          class="search-btn"
          round
          to="/search"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <!-- 频道切换列表 tab 栏 -->
    <van-tabs v-model="activeName" sticky class="channelTabs">
      <!-- 占位元素，这样可以使汉堡图标不遮挡该有的tab标签 -->
      <div class="fill" slot="nav-right"></div>
      <div slot="nav-right" class="wapIcon" @click="isPopup = true">
        <van-icon name="wap-nav" />
      </div>
      <van-tab
        v-for="channel in channels"
        :key="channel.id"
        :title="channel.name"
      >
        <!-- 自定义组件来显示文章列表 -->
        <!-- 当查看后才会渲染当前的组件 -->
        <ArticalList :channel="channel"></ArticalList
      ></van-tab>
    </van-tabs>

    <!-- 频道编辑弹出层popup
        弹出位置 高度 是否可关闭 关闭按钮位置 圆角设置 挂载位置（渲染出的真是dom节点的位置）
    -->
    <van-popup
      v-model="isPopup"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
      round
      teleport="body"
    >
      <!-- 弹出层中封装组件 -->
      <ChannelEdit
        :channels="channels"
        :activeName="activeName"
        @updateActiveName="activeName = activeName - 1"
        @changeChannel="changeChannel"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import ArticalList from './components/artical-list'
import ChannelEdit from './components/ChannelEdit'
export default {
  name: 'HomeIndex',
  components: {
    ArticalList,
    ChannelEdit
  },
  data () {
    return {
      // 默认激活的标签
      activeName: 0,
      // 频道列表信息
      channels: [],
      // 控制弹出层
      isPopup: false
    }
  },
  methods: {
    async loadChannels () {
      try {
        const { data: res } = await this.$https.get('/app/v1_0/user/channels')
        this.channels = res.data.channels
      } catch (error) {
        this.$toast.fail('获取频道信息失败！')
      }
    },
    changeChannel (index) {
      this.activeName = index
      this.isPopup = false
    }
  },
  created () {
    this.loadChannels()
  }
}
</script>

<style scoped lang="less">
/deep/.van-nav-bar__title {
  max-width: none;
  .search-btn {
    width: 278px;
    height: 27px;
    background-color: #0f84ff;
  }
  .van-icon-search {
    color: #fff;
    font-size: 18px;
  }
}
/deep/.van-tabs__line {
  width: 17px !important;
  height: 3px;
  background-color: #3296fa;
  margin-bottom: 4px;
}
/deep/.van-tab {
  width: 80px;
  border-right: 1px solid #eee;
  border-bottom: 1px solid #eee;
}
/deep/.van-tabs__nav--complete {
  padding-right: unset;
}
.wapIcon {
  position: fixed;
  right: 0px;
  height: 44px;
  background-color: #fff;
  opacity: 0.9;
  width: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.fill {
  width: 36px;
  // 父元素的其他子元素已经通过flex分取了宽度
  flex-shrink: 0;
}
</style>
